package com.lykj.llw.box.ui.activity

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.KeyboardUtils
import com.dinuscxj.itemdecoration.LinearDividerItemDecoration
import com.gyf.immersionbar.BarHide
import com.gyf.immersionbar.ImmersionBar
import com.lykj.lib_base.base.BaseActivity
import com.lykj.lib_base.constants.BConstants
import com.lykj.lib_base.internal.extfun.clickWithTrigger
import com.lykj.lib_base.internal.utils.ToastUtils
import com.lykj.lib_base.net.http.CustomResponseBodyObserver
import com.lykj.lib_base.net.http.RetrofitManager
import com.lykj.llw.box.R
import com.lykj.llw.box.adapter.MyBillingAdapter
import com.lykj.llw.box.data.bean.response.MyBillingListResponse
import com.lykj.llw.box.databinding.ActMyBillingBinding
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import io.reactivex.disposables.Disposable
import okhttp3.ResponseBody

/**
 * ================================================
 * 我的账单页面
 * ================================================
 *
 * @author gosha
 * @date 2023/10/16-16:02
 */
class MyBillingActivity : BaseActivity() {
    lateinit var binding: ActMyBillingBinding

    private val mAdapter = MyBillingAdapter()
    private val mData = ArrayList<MyBillingListResponse.DataDTO.RowsDTO>()
    private var pageSize = 10
    private var totalPage = 1
    private var currentPage = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = ActMyBillingBinding.inflate(layoutInflater)
        setContentView(binding.root)
        initUI()
        initData()
        setListener()
    }

    override fun onResume() {
        super.onResume()

        ImmersionBar.with(this).transparentBar()
            .statusBarDarkFont(true) //状态栏字体是深色，不写默认为亮色
            .navigationBarDarkIcon(true)
            .hideBar(BarHide.FLAG_HIDE_NAVIGATION_BAR)
            .navigationBarEnable(false)
            .init()
    }

    override fun initUI() {
        binding.titleBar.tvTitle.text = "我的账单"
        binding.titleBar.tvRightOption.visibility = View.INVISIBLE

        initRecyclerView()

        if (!BConstants.isDebug) {
            binding.lltSearch.visibility = View.GONE
        }
    }

    //获取钱包记录
    private fun obtainMyBilling() {
        clearBodyParam()
        initBodyParam()
        bodyParams["pageNo"] = currentPage
        bodyParams["pageSize"] = pageSize
        RetrofitManager.instance.obtainMyBilling(bodyParams,object :
            CustomResponseBodyObserver<ResponseBody>() {
            override fun onSubscribe(d: Disposable) {
                addDisposables(this, d)
            }

            override fun specificHandle(responseBody: String) {
                val bean =
                    GsonUtils.fromJson(responseBody, MyBillingListResponse::class.java)
                totalPage = bean.data.totalPage
                if (!bean.data.rows.isNullOrEmpty()) {
                    mData.addAll(bean.data.rows)
                }

                mAdapter.notifyDataSetChanged()
            }

            override fun handleException(ret: String, msg: String?) {
                msg?.let {
                    ToastUtils.showShort(this@MyBillingActivity, it)
                }
            }
        })
    }

    override fun initData() {
        obtainMyBilling()
    }

    private fun initRecyclerView() {
        val linearLayoutManager = LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        binding.rcvBilling.layoutManager = linearLayoutManager
        binding.rcvBilling.setHasFixedSize(true)
        val dividerItemDecoration = LinearDividerItemDecoration(
            this, LinearDividerItemDecoration.LINEAR_DIVIDER_VERTICAL
        )
        dividerItemDecoration.setDivider(
            resources.getDrawable(
                R.drawable.divider_0dp, resources.newTheme()
            )
        )
        binding.rcvBilling.addItemDecoration(dividerItemDecoration)
        mAdapter.data = mData
        val emptyView = View.inflate(this, R.layout.empty_playing, null)
        emptyView.findViewById<TextView>(R.id.tv_desc).text = "暂无账单数据"
        mAdapter.setEmptyView(emptyView)
        binding.rcvBilling.adapter = mAdapter
    }

    override fun setListener() {
        binding.titleBar.ivBack.clickWithTrigger {
            finish()
        }

        binding.tvSearch.clickWithTrigger {
            ToastUtils.showShort(this, "点击了搜索")
        }

        binding.srfRefresh.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                currentPage = 1
                mData.clear()
                mAdapter.notifyDataSetChanged()

                obtainMyBilling()
                refreshLayout.finishRefresh(300)
                refreshLayout.resetNoMoreData()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                if (currentPage == totalPage) {
                    refreshLayout.finishLoadMoreWithNoMoreData()
                    ToastUtils.showShort(this@MyBillingActivity, "没有更多数据了")
                    return
                }
                refreshLayout.finishLoadMore(500, true, false)
                currentPage++

                obtainMyBilling()
            }
        })

        KeyboardUtils.registerSoftInputChangedListener(this) { height ->
            if (height == 0) {//软键盘收起,移除焦点
                binding.etSearch.clearFocus()
            }
        }
    }

    override fun onDestroy() {
        removeDisposables(this)
        super.onDestroy()
    }
}